<script setup>
import { computed } from 'vue'
import { Icons } from '@/constants'
import { JBoltApi } from '@/service/request'

const props = defineProps({
    types: {
        type: Array,
        default: () => []
    },
    serverUrl: String,
    tableInstance: Object,
    fileName: [String, Function],
    serverForm: Object,
    tableExportConfig: {
        type: Object,
        default: () => ({})
    }
})

const options = computed(() => {
    let types = props.types
    if (!types) {
        types = []

        if (props.serverUrl) {
            types.push('server')
        }
        if (props.tableInstance) {
            types.push('table')

            const cols = props.tableInstance.getColumns()
            if (cols.some((col) => col.type == 'checkbox')) {
                types.push('row')
            }
        }
    }

    return types.map((type) => {
        return {
            label:
                type === 'server'
                    ? '导出所有数据'
                    : type == 'table'
                    ? '导出当前表格数据'
                    : '导出选中行数据',
            key: type
        }
    })
})

const exportConfig = computed(() => {
    const { checkEmpty, ...config } = props.tableExportConfig ?? {}
    return {
        columnFilterMethod({ column }) {
            return column.field && !column.type
        },
        checkEmpty: checkEmpty ?? true,
        ...config
    }
})

function doExport(key) {
    switch (key) {
        case 'server':
            exportFromServer()
            break
        case 'table':
            exportFromTable()
            break
        case 'row':
            exportFromCheckedRows()
            break
    }
}

function exportFromServer() {
    if (!props.serverUrl) {
        window.$error('请设置serverUrl属性')
        return
    }
    let fileName = ''
    if (typeof props.fileName == 'string') {
        fileName = props.fileName
    }
    JBoltApi.download(props.serverUrl, fileName, props.serverForm)
}

function exportFromTable() {
    if (!props.tableInstance) {
        window.$error('请设置tableInstance属性')
        return
    }
    if (!props.tableInstance.getData(0) && exportConfig.value.checkEmpty) {
        window.$error('没有可导出的数据')
        return
    }
    props.tableInstance.exportData({
        filename: props.fileName ?? 'data',
        type: 'csv',
        download: true,
        ...exportConfig.value
    })
}

function exportFromCheckedRows() {
    if (!props.tableInstance) {
        window.$error('请设置tableInstance属性')
        return
    }
    const cols = props.tableInstance.getColumns()
    if (cols.some((col) => col.type == 'checkbox') == false) {
        window.$error('未检测到表格有可选列，无法导出')
        return
    }
    const data = props.tableInstance.getCheckboxRecords()
    if ((!data || data.length == 0) && exportConfig.value.checkEmpty) {
        window.$error('请先选择要导出的数据')
        return
    }
    props.tableInstance.exportData({
        filename: props.fileName ?? 'data',
        type: 'csv',
        download: true,
        data: props.tableInstance.getCheckboxRecords(),
        ...exportConfig.value
    })
}
</script>

<template>
    <n-dropdown :options="options" @select="doExport">
        <slot>
            <jb-btn ghost type="warning" :icon="Icons.DOWNLOAD"> 导出excel </jb-btn>
        </slot>
    </n-dropdown>
</template>

<style scoped></style>
